package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.audit.baidu.BaiduTextScan;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.NewShenHe;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @Description NewShenHeImpl
 * @Author liujiangyuan
 * @Date 2024-11-27-10:17
 */
@Service
public class NewShenHeImpl implements NewShenHe {
    @Autowired
    private BaiduTextScan baiduTextScan;
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public Boolean shenHe(WmNews wmNews) {
        String content1 = wmNews.getContent();
        JSONArray objects1 = JSON.parseArray(content1);
        Integer integer = 0;
        for (Object object : objects1) {
            JSONObject jsonObject = (JSONObject) object;
            if (jsonObject.get("type").equals("text")) {
                String text = jsonObject.getString("value");
                Integer integer1 = baiduTextScan.textScan(text);
                integer=integer1;
            }
        }
        LambdaUpdateWrapper<WmNews> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WmNews::getId,wmNews.getId());

        if (integer==1){
            if (new Date().after(wmNews.getPublishTime()) ){

                lambdaUpdateWrapper.set(WmNews::getStatus,9);
            }else {
                lambdaUpdateWrapper.set(WmNews::getStatus,8);
            }
            wmNewsMapper.update(wmNews,lambdaUpdateWrapper);
            Long l = new Long(0);
            long time = new Date().getTime();
            if (new Date().before(wmNews.getPublishTime())){
                l=wmNews.getPublishTime().getTime()-time;

                String message = wmNews.getId().toString();
                Message messageObject = MessageBuilder.withBody(message.getBytes(StandardCharsets.UTF_8))
                        .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                        .setExpiration("500000")
                        .setHeader("x-delay",10000)
                        .build();
                rabbitTemplate.convertAndSend("DELAY_EXCHANGE" , "DELAY_ROUTE_KEY", messageObject);
            }

            return true;
        }else {
            return false;
        }

    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "DELAY_QUEUE"),
            exchange = @Exchange(name = "DELAY_EXCHANGE",
                    type = ExchangeTypes.TOPIC, delayed = "true"),
            key = "DELAY_ROUTE_KEY"))
    public void listenDelayMessage(String id){
        WmNews wmNews = wmNewsMapper.selectById(id);
        LambdaUpdateWrapper<WmNews> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WmNews::getId,id);
        lambdaUpdateWrapper.set(WmNews::getStatus,9);
        wmNewsMapper.update(wmNews,lambdaUpdateWrapper);
    }

}
